using System;


namespace Monorf.Network
{

    public class NetworkBuffer
    {

        private Int32 m_messageSize;
        private Int64 bufferSize = 0;
        private Byte[] m_buffer;

        public NetworkBuffer(Int32 messageSize)
        {
            m_messageSize = messageSize;
        }

        public NetworkBuffer(Int32 messageSize, Int64 bufferSize)
        {
            this.messageSize = messageSize;
            this.bufferSize = bufferSize;
        }

        #region properties

        /// <summary>
        /// message size 
        /// </summary>
        public Int32 MessageSize { get { return m_messageSize; } }

        /// <summary>
        /// pool buffer zise
        /// </summary>
        //public Int64 BufferSize { get { return bufferSize; } }

        #endregion

        /// <summary>
        /// create buffer to receive messages from network device
        /// </summary>
        /// <param name="messageSize"></param>
        /// <param name="bufferSize"></param>
        public Byte[] CreateBuffer()
        {
            if (m_buffer == null)
                m_buffer = new Byte[m_messageSize];
            return m_buffer;
        }

        /// <summary>
        /// get current buffer data
        /// </summary>
        /// <returns></returns>
        public void GetBuffer(Byte[] bufferData)
        {

            if (m_buffer == null)
                return;

            if (bufferData.Length > bufferSize)
                throw new ArgumentOutOfRangeException();

            Array.Copy(m_buffer, bufferData, m_buffer.Length);

        }

        /// <summary>
        /// clear buffer data
        /// </summary>
        public void Clear()
        {
            if (m_buffer != null)
                Array.Clear(m_buffer, 0, m_buffer.Length);

        }
    }
}
